package com.qf.controller;

import com.qf.bean.SysUserInfo;
import com.qf.dto.R;
import com.qf.dto.UserDto;
import com.qf.service.SysUserInfoService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

/*
 *  @author Administrator
 *  @version V1.0
 *  @Project smartdeviceplatgorm
 *  @Package com.qf.controller
 *  @Description
 *  @Date2022/3/14 0014 17:21
 */
@RestController
@RequestMapping("/user")
public class SysUserInfoController {
    private SysUserInfoService sysUserInfoService;
    @Autowired
    public void setSysUserInfoService(SysUserInfoService sysUserInfoService) {
        this.sysUserInfoService = sysUserInfoService;
    }
    @PostMapping("/login")
    public R login(@RequestBody UserDto userDto, HttpSession session) {
        SysUserInfo sysUserInfo = sysUserInfoService.findByUsernameAndPassword(userDto.getUsername(), userDto.getPassword());
        session.setAttribute("user",sysUserInfo);
        return R.setOk();
    }
    @PostMapping("/regist")
    public R registry(@RequestBody UserDto userDto) {
        //把userDto 转到sysUserInfo中
        System.err.println(userDto);
        SysUserInfo sysUserInfo = new SysUserInfo();
        BeanUtils.copyProperties(userDto,sysUserInfo);//拷贝对象,会将相同属性的内容复制到新的对象上面
        sysUserInfoService.addSysUserInfo(sysUserInfo);
        return R.setOk();
    }
    @PostMapping("/{userId}")
    public R findById(@PathVariable Integer userId) {
        return R.setOk(sysUserInfoService.findById(userId));
    }

    @PostMapping("/update")
    public R updateSysUserInfo(@RequestBody UserDto userDto) {
        SysUserInfo sysUserInfo=new SysUserInfo();
        BeanUtils.copyProperties(userDto,sysUserInfo);
        sysUserInfoService.updateSysUserInfo(sysUserInfo);
        System.err.println(sysUserInfo);
        return R.setOk();
    }

    @PostMapping("/updatepwd")
    public R updatePwd(@RequestBody UserDto userDto) {
        SysUserInfo sysUserInfo=new SysUserInfo();
        BeanUtils.copyProperties(userDto,sysUserInfo);
        sysUserInfoService.updateSysUserInfo(sysUserInfo);
        System.err.println(sysUserInfo);
        return R.setOk();
    }

}
